package orz.itrip.search.service.impl;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import orz.itrip.DtoUtils;
import orz.itrip.dto.Dto;
import orz.itrip.search.service.SearchHotel;
import orz.itrip.search.vo.HotelVo;
import orz.itrip.PageUtils;
import orz.itrip.search.vo.SolrHotelBean;

@Service
public class SearchHotelImpl implements SearchHotel {

    @Override
    public Dto searchItripHotelPage(HotelVo hotelVo) {
        SolrTemplate solrTemplate=new ClassPathXmlApplicationContext("spring-solr.xml").getBean(SolrTemplate.class);
        Query query=new SimpleQuery("*:*");
        Criteria criteria=new Criteria("id");
        if (!StringUtils.isEmpty(hotelVo.getDestination())){
            criteria = criteria.and("redundantCityName").is(hotelVo.getDestination());
        }
        if (!StringUtils.isEmpty(hotelVo.getKeyword())){
            criteria = criteria.and("keyword").contains(hotelVo.getKeyword());
        }
        if (!StringUtils.isEmpty(hotelVo.getFeatureIds())){
            criteria = criteria.and("featureIds").contains(hotelVo.getFeatureIds());
        }
        if (hotelVo.getHotelLevel()!=null&&hotelVo.getHotelLevel()!=0){
            criteria = criteria.and("hotelLevel").is(hotelVo.getHotelLevel().toString());
        }
        if (hotelVo.getMinPrice()!=null&&hotelVo.getMaxPrice()!=null&&hotelVo.getMaxPrice()!=0){
            criteria = criteria.and("minPrice").between(hotelVo.getMinPrice(),hotelVo.getMaxPrice());
        }
        if (!StringUtils.isEmpty(hotelVo.getTradeAreaIds())){
            criteria = criteria.and("tradeAreaIds").contains(hotelVo.getTradeAreaIds());
        }
        if (!StringUtils.isEmpty(hotelVo.getAscSort())){
            query.addSort(new Sort(new Sort.Order(Sort.Direction.ASC,hotelVo.getAscSort())));
        }
        if (!StringUtils.isEmpty(hotelVo.getDescSort())){
            query.addSort(new Sort(new Sort.Order(Sort.Direction.DESC,hotelVo.getDescSort())));
        }
        query.addCriteria(criteria);
        ScoredPage<SolrHotelBean> page = solrTemplate.queryForPage("hotel",query,SolrHotelBean.class);
        PageUtils<SolrHotelBean> data = getSolrHotelBeanPageUtils(page);
        return DtoUtils.getSuccesWithData(data,null);
    }

    @Override
    public Dto searchItripHotelListByHotCity(Integer cityId, Integer count) {
        SolrTemplate solrTemplate=new ClassPathXmlApplicationContext("spring-solr.xml").getBean(SolrTemplate.class);
        Query query=new SimpleQuery("*:*");
        Criteria criteria=new Criteria("id");
        if (cityId!=null&&cityId!=0){
            criteria = criteria.and("cityId").is(cityId.toString());
        }
        if (count!=null&&count!=0){
            query.setRows(count);
        }else {
            query.setRows(6);
        }
        query.addCriteria(criteria);
        ScoredPage<SolrHotelBean> page = solrTemplate.queryForPage("hotel",query,SolrHotelBean.class);
        PageUtils<SolrHotelBean> data = getSolrHotelBeanPageUtils(page);
        return DtoUtils.getSuccesWithData(data.getRows(),null);
    }

    private PageUtils<SolrHotelBean> getSolrHotelBeanPageUtils(ScoredPage<SolrHotelBean> page) {
        PageUtils<SolrHotelBean> data = new PageUtils<>();
        data.setPageCount(page.getTotalPages());
        data.setBeginPos(0);
        data.setCurPage(1);
        data.setRows(page.getContent());
        data.setTotal(page.getTotalElements());
        data.setPageSize(10);
        return data;
    }
}
